package com.hrm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hrm.dto.EmpDepDTO;
import com.hrm.entity.Employee;
import com.hrm.entity.MyPage;
import com.hrm.service.DepartmentService;
import com.hrm.service.EmployeeService;
import com.hrm.service.SystemService;
import com.hrm.utils.JsonMsg;
import com.hrm.utils.Layui;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Auther:
 * @Date: 2020/5/24 21:40
 * @Description: 系统设置
 */
@Controller
@RequestMapping("/sysconfig")
public class SystemConfigController extends BaseController {
    @Autowired
    SystemService systemService;
    @Autowired
    EmployeeService employeeService;
    @Autowired
    DepartmentService departmentService;


    @RequestMapping("/authority.html")
    public String Authority(){
        return "system/authority";
    }

    /**
     * 备份数据库
     * @return: JsonMsg
     */
    @RequestMapping("/backupDB.html")
    @ResponseBody
    public JsonMsg backupDB(){
        if(systemService.backupDB(session)){
            return JsonMsg.success();
        }else return JsonMsg.fail().addInfo("BACKUP_ERROR","备份数据库失败");
    }

    /**
     * 还原数据库
     * @return: JsonMsg
     */
    @RequestMapping("/restoreDB.html")
    @ResponseBody
    public JsonMsg restoreDB(){
        if(systemService.restoreDB(session)){
            return JsonMsg.success();
        }else return JsonMsg.fail().addInfo("RESTORE_ERROR","还原数据库失败");
    }

    @RequestMapping("/empWithDepList.html")
    @ResponseBody
    public ResponseEntity<Layui> empWithDepList(MyPage myPage){
        PageHelper.startPage(myPage.getPageNum(),myPage.getPageSize());
        List<EmpDepDTO> empDepDTOS = employeeService.empDepList();
        PageInfo pageInfo = new PageInfo(empDepDTOS,5);
        Layui layui = Layui.data(pageInfo.getTotal(),pageInfo.getList());
        return ResponseEntity.ok(layui);
    }

    @RequestMapping("/queryEmpDep.html")
    @ResponseBody
    public ResponseEntity<Layui> queryEmpDep(MyPage myPage, Employee employee){
        PageHelper.startPage(myPage.getPageNum(),myPage.getPageSize());
        List<EmpDepDTO> empDepDTOS = employeeService.queryEmpDepByName(employee);
        PageInfo pageInfo = new PageInfo(empDepDTOS,5);
        Layui layui = Layui.data(pageInfo.getTotal(),pageInfo.getList());
        return ResponseEntity.ok(layui);
    }

    @RequestMapping("/edit.html")
    public String toEditAuthority(Model model, EmpDepDTO empDepDto){
        model.addAttribute("empInfo",empDepDto);
        return "system/edit";
    }

    @RequestMapping("/getJobTitle.html")
    @ResponseBody
    public JsonMsg jobTitleList(){
        List<String> jobTitleList = employeeService.jobTitleList();
        return JsonMsg.success().addInfo("jobTitleList",jobTitleList);
    }

    @RequestMapping("/updateJobTitle.html")
    @ResponseBody
    public JsonMsg updateJobTitle(Employee employee){
        employeeService.updateEmp(employee);
        return JsonMsg.success();
    }
}
